package org.dromara.creditcard.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.creditcard.domain.CreditMer;
import org.dromara.creditcard.domain.CreditOutRecord;
import org.dromara.creditcard.domain.CreditPackage;
import org.dromara.creditcard.service.ICreditPackageService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysRole;
import org.dromara.system.service.ISysRoleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;

/**
 * 养卡套餐
 *
 * @author Mr.Zhou
 * @date 2023-11-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/credit/package")
public class CreditPackageController extends BaseController {

    private final ICreditPackageService creditPackageService;

    private final ISysRoleService sysRoleService;

    /**
     * 查询养卡套餐列表
     */
    @SaCheckPermission("credit:package:list")
    @GetMapping("/list")
    public TableDataInfo<CreditPackage> list(CreditPackage creditPackage, PageQuery pageQuery) {
        Integer agentId = null;
        LoginUser loginUser = LoginHelper.getLoginUser();
        SysRole sysRole = sysRoleService.selectRoleByUserId(loginUser.getUserId());
        if(sysRole != null){
            if(sysRole.getRoleKey().equals("agent") || sysRole.getRoleKey().equals("business")) {
                agentId = loginUser.getAgentId();
            }
        }

        QueryWrapper<CreditPackage> qw = new QueryWrapper<>();
        if(agentId != null){
            qw.eq("agent_id", agentId);
        }else{
            if(creditPackage.getAgentId() != null){
                qw.eq("agent_id", creditPackage.getAgentId());
            }
        }

        qw.orderByDesc("id");
        TableDataInfo<CreditPackage> td = PageUtils.buildDataInfo(creditPackageService.page(PageUtils.buildPage(),qw));
        List<CreditPackage> list = td.getRows();
        return td;
    }

    /**
     * 养卡套餐列表(不分页，下拉框使用)
     */
    @SaCheckPermission("credit:package:list")
    @GetMapping("/noPage/list")
    public R<List<CreditPackage>> noPageList(CreditPackage creditPackage) {
        List<CreditPackage> list = creditPackageService.list();
        return R.ok(list);
    }



    /**
     * 导出养卡套餐列表
     */
    @SaCheckPermission("credit:package:export")
    @Log(title = "养卡套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CreditPackage creditPackage, HttpServletResponse response) {
        List<CreditPackage> list = creditPackageService.list();
        ExcelUtil.exportExcel(list, "养卡套餐", CreditPackage.class, response);
    }

    /**
     * 获取养卡套餐详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("credit:package:query")
    @GetMapping("/{id}")
    public R<CreditPackage> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(creditPackageService.getById(id));
    }

    /**
     * 新增养卡套餐
     */
    @SaCheckPermission("credit:package:add")
    @Log(title = "养卡套餐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CreditPackage creditPackage) {

        LoginUser loginUser = LoginHelper.getLoginUser();
        creditPackage.setAgentId(loginUser.getAgentId());

        return toAjax(creditPackageService.save(creditPackage));
    }

    /**
     * 修改养卡套餐
     */
    @SaCheckPermission("credit:package:edit")
    @Log(title = "养卡套餐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CreditPackage creditPackage) {
        return toAjax(creditPackageService.updateById(creditPackage));
    }

    /**
     * 删除养卡套餐
     *
     * @param ids 主键串
     */
    @SaCheckPermission("credit:package:remove")
    @Log(title = "养卡套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(creditPackageService.removeByIds(Arrays.asList(ids)));
    }
}
